package com.myx.dao.news;

import com.myx.pojo.News;
import com.myx.util.JdbcUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements NewsDao{
    private final JdbcUtil jdbcUtil=new JdbcUtil();
    @Override
    public List<News> getNewsListPages(Integer currentNo, Integer pageSize) {
        String sql="select * from news_detail order by createDate desc limit ?,?";
        Object[] params={(currentNo-1)*pageSize,pageSize};
        ResultSet resultSet = jdbcUtil.getObjectByParams(sql, params);
        ArrayList<News> newsList=new ArrayList();
        try {
            while (resultSet.next()){
                Integer id=resultSet.getInt("id");
                Integer categoryId = resultSet.getInt("categoryId");
                String title = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String content = resultSet.getString("content");
                String picPath = resultSet.getString("picPath");
                Integer createBy = resultSet.getInt("createBy");
                Date createDate = resultSet.getDate("createDate");
                Date modifyDate = resultSet.getDate("modifyDate");
                Integer modifyBy = resultSet.getInt("modifyBy");
                News news=new News();
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setPicPath(picPath);
                news.setCreateBy(createBy);
                news.setCreateDate(createDate);
                news.setModifyDate(modifyDate);
                news.setModifyBy(modifyBy);
                newsList.add(news);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.jdbcUtil.closeResource();
        }
            return newsList;
    }




    public News getNewsById(int newsId) {
        String sql="select * from news_detail where id=?";
        Object[] params={newsId};
        ResultSet resultSet = this.jdbcUtil.getObjectByParams(sql, params);
        News news=new News();
        try {
            while (resultSet.next()){
                Integer id=resultSet.getInt("id");
                Integer categoryId = resultSet.getInt("categoryId");
                String title = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String content = resultSet.getString("content");
                String picPath = resultSet.getString("picPath");
                Integer createBy = resultSet.getInt("createBy");
                Date createDate = resultSet.getDate("createDate");
                Date modifyDate = resultSet.getDate("modifyDate");
                Integer modifyBy = resultSet.getInt("modifyBy");
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setPicPath(picPath);
                news.setCreateBy(createBy);
                news.setCreateDate(createDate);
                news.setModifyDate(modifyDate);
                news.setModifyBy(modifyBy);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.jdbcUtil.closeResource();
        }
        return news;
    }


    @Override
    public List<News> getNewsListPagesByCategoryId(Integer categoryId, Integer currentNo, Integer pageSize) {
        String sql="select * from news_detail where categoryId=? order by createDate desc limit ?,?";
        Object[] params={categoryId,(currentNo-1)*pageSize,pageSize};
        ResultSet resultSet = this.jdbcUtil.getObjectByParams(sql, params);
        ArrayList<News> newsList=new ArrayList();
        try {
            while (resultSet.next()){
                Integer id=resultSet.getInt("id");
                Integer categoryId2 = resultSet.getInt("categoryId");
                String title = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String content = resultSet.getString("content");
                String picPath = resultSet.getString("picPath");
                Integer createBy = resultSet.getInt("createBy");
                Date createDate = resultSet.getDate("createDate");
                Date modifyDate = resultSet.getDate("modifyDate");
                Integer modifyBy = resultSet.getInt("modifyBy");
                News news=new News();
                news.setId(id);
                news.setCategoryId(categoryId2);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setPicPath(picPath);
                news.setCreateBy(createBy);
                news.setCreateDate(createDate);
                news.setModifyDate(modifyDate);
                news.setModifyBy(modifyBy);
                newsList.add(news);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.jdbcUtil.closeResource();
        }
        return newsList;
    }

    @Override
    public boolean addNews(News news) {
        String sql="insert into news_detail(categoryId,title,summary,content,picPath,createBy,createDate)values(?,?,?,?,?,?,?)";
        Object[] params={news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getPicPath(),news.getCreateBy(),news.getCreateDate()};
        return this.jdbcUtil.addDeleteModify(sql, params);
    }

    @Override
    public boolean modifyNews(News news) {
        String sql="update news_detail set categoryId=? ,title=?, summary=?,content=?,picPath=? where id=?";
        Object[] params={news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getPicPath(),news.getId()};
        return this.jdbcUtil.addDeleteModify(sql, params);
    }

    @Override
    public boolean deleteNews(Integer newsId) {
        String sql="delete from news_detail where id=?";
        Object[] params={newsId};
        return this.jdbcUtil.addDeleteModify(sql, params);
    }

    @Override
    public List<News> getNewsListPagesByTitle(String title, Integer pageNo, Integer pageSize) {
        String sql="select * from news_detail where title like CONCAT('%',?,'%') limit?,?";
        Object[] params={title,(pageNo-1)*pageSize,pageSize};
        ResultSet resultSet = this.jdbcUtil.getObjectByParams(sql, params);
        ArrayList<News> newsList=new ArrayList();
        try {
            while (resultSet.next()){
                Integer id=resultSet.getInt("id");
                Integer categoryId2 = resultSet.getInt("categoryId");
                String title3 = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String content = resultSet.getString("content");
                String picPath = resultSet.getString("picPath");
                Integer createBy = resultSet.getInt("createBy");
                Date createDate = resultSet.getDate("createDate");
                Date modifyDate = resultSet.getDate("modifyDate");
                Integer modifyBy = resultSet.getInt("modifyBy");
                News news=new News();
                news.setId(id);
                news.setCategoryId(categoryId2);
                news.setTitle(title3);
                news.setSummary(summary);
                news.setContent(content);
                news.setPicPath(picPath);
                news.setCreateBy(createBy);
                news.setCreateDate(createDate);
                news.setModifyDate(modifyDate);
                news.setModifyBy(modifyBy);
                newsList.add(news);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.jdbcUtil.closeResource();
        }
        return newsList;
    }

}
